#include "process.h"
#include <time.h>
#include <stdlib.h>
#include <unistd.h>

double gtotal = 1024.0;
double speed = 1.0;

// 函数指针
typedef void(*callback_t)(double, double);

double SpeedFloat(double start, double range)
{
    int int_range = (int)range;
    return start + rand() % int_range + (range - int_range);
}

// cb：回调函数
void DownLoad(double total, callback_t cb)
{
    srand(time(NULL));
    double curr = 0.0;
    int flag = 1;
    while(curr <= total)
    {
        cb(total, curr); // 更新进度，按照下载进度，进行更新进度条
        curr += SpeedFloat(speed, 4.3); // 模拟下载行为
        usleep(3000);
        if(flag && curr > total)
        {
            flag = 0;
            curr = total;
        }
    }
}

int main()
{
    //Process();
    printf("download：20.0MB\n");
    DownLoad(20.0, FlushProcess);
    printf("download：2015.7MB\n");
    DownLoad(2015.7, FlushProcess);
    printf("download：201.0MB\n");
    DownLoad(201.0, FlushProcess);
    printf("download：39.6MB\n");
    DownLoad(39.6, FlushProcess);
    
    return 0;
}
